package com.example.shixun2;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactViewHolder> {

    private List<Contact> contactList;      //联系人列表
    private Context context;              //上下文

    public ContactAdapter(List<Contact> contactList, Context context) {
        this.contactList = contactList;
        this.context = context;
    }

    // 创建ViewHolder
    public class ContactViewHolder extends RecyclerView.ViewHolder {
        //组件的头部资源的声明
        RelativeLayout clickableHeader;     //头部的布局
        ImageView avatar;                  //头像图片
        TextView contactName;             //联系人昵称
        ImageView expandArrow;            //拓展的那个资源图片

        //可展开内容的声明
        LinearLayout expandableContent;     //展开内容
        RecyclerView messageHistory;        //内部的RecyclerView
        public ContactViewHolder(@NonNull View itemView) {
            super(itemView);
            //头部
            clickableHeader = itemView.findViewById(R.id.clickable_header);
            avatar = itemView.findViewById(R.id.iv_avatar);
            contactName = itemView.findViewById(R.id.tv_contact_name);
            expandArrow = itemView.findViewById(R.id.iv_expand_arrow);
            //可展开内容
            expandableContent = itemView.findViewById(R.id.expandable_content);
            messageHistory = itemView.findViewById(R.id.recycler_view_history);
            //在 ViewHolder 创建时就设置好内部 RecyclerView 的 LayoutManager
            messageHistory.setLayoutManager(new LinearLayoutManager(context));
        }
    }
    // 创建ViewHolder
    @NonNull
    @Override
    public ContactAdapter.ContactViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item_contact_message, parent, false);
        return new ContactViewHolder(view);
    }

    // 绑定数据
    @Override
    public void onBindViewHolder(@NonNull ContactAdapter.ContactViewHolder holder, int position) {
        Contact contact = contactList.get(position);        //获取当前位置的 Contact 对象
        //绑定头部数据
        holder.contactName.setText(contact.getName());
        holder.avatar.setImageResource(contact.getAvatarResId());
        //根据 isExpanded 状态，决定显示/隐藏
        holder.expandableContent.setVisibility(contact.isExpanded() ? View.VISIBLE : View.GONE);
        //绑定可展开内容
        MessageHistoryAdapter messageHistoryAdapter = new MessageHistoryAdapter(contact.getMessageHistory());
        holder.messageHistory.setAdapter(messageHistoryAdapter);
        //根据 isExpanded 状态，决定箭头旋转角度
        holder.expandArrow.setRotation(contact.isExpanded() ? 180f : 0f);
        //绑定头部点击事件
        holder.clickableHeader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 1. 切换数据模型中的状态
                contact.setExpanded(!contact.isExpanded());
                // 2. 通知 Adapter 刷新“这一个”列表项
                notifyItemChanged(holder.getAdapterPosition());
            }
        });
    }

    @Override
    public int getItemCount() {
        return contactList.size();
    }
}
